package com.example.guiguiapplicatio.fragment.Fulfillment.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.Serviceitems;
import com.example.guiguiapplicatio.Response.ServiceitemsDescriptionTotal;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class FulfillmentitemsAdapter extends RecyclerView.Adapter<FulfillmentitemsAdapter.FulfillmentitemsViewHolder>{

    private List<Integer> selectedPositions = new ArrayList<>();
    private Context mContext;
    private int mLayoutId;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private List<ServiceitemsDescriptionTotal> serviceitemsList;
    public FulfillmentitemsAdapter(List<ServiceitemsDescriptionTotal> serviceitemslist, int layoutId, Context context) {
        this.serviceitemsList = serviceitemslist;
        mLayoutId = layoutId;
        mContext = context;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FulfillmentitemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fulfillment_detailed, parent, false);
        return new FulfillmentitemsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FulfillmentitemsViewHolder holder, int position) {
        ServiceitemsDescriptionTotal serviceitems =serviceitemsList.get(position);
        holder.itemheadwriting1TextView.setText(serviceitems.getServiceItemsName());
        double ratio;
        if (serviceitems.getTotal() != 0) {
            ratio = (double) serviceitems.getTotal() / 4 * 100;
        } else {
            ratio = 0; // Or any other value you prefer when the denominator is zero
        }
        String ratioText = String.format("%.2f%%", ratio);
        holder.item_head_probability.setText(ratioText);
        if(serviceitems.getFulfillmentNexttime() == null){
                holder.item_main_next_start_Fulfillment_time.setText("没有开始履约");
        }else {
            if(ratio == 100){
                holder.item_main_next_start_Fulfillment_time.setText("已经完成履约");
            }
            else {
            String NextTime = dateFormat.format(serviceitems.getFulfillmentNexttime());
            holder.item_main_next_start_Fulfillment_time.setText(NextTime);
            }
        }
        if(ratio == 0 ){
            holder.item_main_writing2.setText("未履约");
        }else if (ratio == 100){
                holder.item_main_writing2.setText("已履约");
        }else {
            holder.item_main_writing2.setText("履约中");
        }
        holder.llitemitemheadTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", serviceitems.getIdcard());
                bundle.putString("servicePackagesId", String.valueOf(serviceitems.getServiceid()));
                bundle.putString("serviceItemId", serviceitems.getServiceItemsId());
                Navigation.findNavController(v).navigate(R.id.action_FulfillmenthomeFragment_to_FulfillmentitemFragment, bundle);
            }
        });
    }



    public void setData(List<ServiceitemsDescriptionTotal> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        this.serviceitemsList = data;
    }
    @Override
    public int getItemCount() {
        return serviceitemsList.size();
    }

    public static class FulfillmentitemsViewHolder extends RecyclerView.ViewHolder {
        TextView itemheadwriting1TextView;
        LinearLayout llitemitemheadTextView;
        TextView item_head_probability;
        TextView item_main_writing2;
        TextView item_main_next_start_Fulfillment_time;

        public FulfillmentitemsViewHolder(@NonNull View itemView) {
            super(itemView);
            itemheadwriting1TextView = itemView.findViewById(R.id.item_head_writing1);
            llitemitemheadTextView = itemView.findViewById(R.id.ll_item_item_head);
            item_head_probability= itemView.findViewById(R.id.item_head_probability);
            item_main_writing2 = itemView.findViewById(R.id.item_main_writing2);
            item_main_next_start_Fulfillment_time= itemView.findViewById(R.id.item_main_next_start_Fulfillment_time);
        }
    }
}
